using UnityEngine;
using System.Collections;

public class Shield : MonoBehaviour
{
		 bool fadeBackIn;
		bool alreadyFading=false;

	// Use this for initialization
	void Start ()
	{
		//StartCoroutine (FadeTo (1.0f, 1.0f));
			StartCoroutine (FadeTo (0.5f, .8f,false));
		fadeBackIn = false;
	}
	
	// Update is called once per frame
	void Update ()
	{
		//if (Input.GetKeyUp (KeyCode.T)) {
	
		//}
		//if (Input.GetKeyUp (KeyCode.F'if)) {
		//	StartCoroutine (FadeTo (1.0f, 1.0f));
		//}//
		/*
		if(FadeTo==null){
			Debug.Log("yo");
		}
		*/
		if(fadeBackIn == false && (!alreadyFading)){
			alreadyFading=true;
				StartCoroutine (FadeTo (0.5f, .8f, true));
				//Debug.Log("yo");
		}
		if(fadeBackIn == true && (!alreadyFading)){
			alreadyFading=true;
				//Debug.Log("yo2");
				StartCoroutine (FadeTo (1.0f, 1.0f, false));
		}
	}
	
	IEnumerator FadeTo (float aValue, float aTime, bool fadeIn)
	{
		//Debug.Log("khlk");
		float alpha = transform.renderer.material.color.a;
		for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime) {
			Color newColor = new Color (1, 1, 1, Mathf.Lerp (alpha, aValue, t));
			transform.renderer.material.color = newColor;
			yield return null;
			
		}
		fadeBackIn = fadeIn;
		alreadyFading=false;
	}
}
